﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using hou_elearning.Business;
using hou_elearning.Entity;

public partial class Admin_control_ucQuanlylich : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            napDdlKhoahoc();
            napLichhoc();
        }
        setSelectedIndex();
    }

    /// <summary>
    /// đặt selected item cho dropdownlist theo ID đã lưu trong session
    /// </summary>
    /// <author>liemqv</author>
    /// <create>08/06/2012</create>
    /// 
    public void setSelectedIndex()
    {
        if (Session["PK_iKhoahocID_Lich"] != null)
        {
            ddlKhoahocxem.SelectedItem.Selected = false;
            ddlKhoahocxem.Items.FindByValue(Session["PK_iKhoahocID_Lich"].ToString()).Selected = true;
        }
        else
        {
            Session["PK_iKhoahocID_Lich"] = ddlKhoahocxem.SelectedValue;
        }
        //Nạp danh sách bài học theo khóa học đã chọn
        Int64 PK_iKhoahocID = Int64.Parse(Session["PK_iKhoahocID_Lich"].ToString());
        List<Khoahoc_BaihocEntity> lstKhoahocBaihoc = Khoahoc_BaihocBRL.GetByFK_iKhoahocID(PK_iKhoahocID);
        List<BaihocEntity> lstBaihoc = new List<BaihocEntity>();
        foreach(Khoahoc_BaihocEntity oKhoahocBaihoc in lstKhoahocBaihoc)
        {
            lstBaihoc.Add(BaihocBRL.GetOne(oKhoahocBaihoc.FK_iBaihocID));
        }
        ddlBaihoc.DataSource = lstBaihoc;
        ddlBaihoc.DataTextField = "sTenbaihoc";
        ddlBaihoc.DataValueField = "PK_iBaihocID";
        ddlBaihoc.DataBind();
        lstBaihoc = null;
        lstKhoahocBaihoc = null;
    }

    /// <summary>
    /// Nạp DropDownList khóa học
    /// </summary>
    /// <author>liemqv</author>
    /// <create>07/06/2012</create>
    /// 
    public void napDdlKhoahoc()
    {
        List<KhoahocEntity> lstKhoahoc = KhoahocBRL.GetAll();
        ddlKhoahocxem.DataSource = lstKhoahoc;
        ddlKhoahocxem.DataTextField = "sTenkhoahoc";
        ddlKhoahocxem.DataValueField = "PK_iKhoahocID";
        ddlKhoahocxem.DataBind();
        lstKhoahoc = null;
    }

    /// <summary>
    /// Lấy ID người dùng đăng nhập
    /// </summary>
    /// <author>liemqv</author>
    /// <create>25/04/2012</create>
    /// 
    public Int64 iGetUserLogin()
    {
        Int64 PK_iNguoidungID = 0;
        try
        {
            if (Session["UserID"] != null)
            {
                PK_iNguoidungID = Int64.Parse(Session["UserID"].ToString());
            }
            else
            {
                Response.Redirect("~/Admin/Login.aspx");
            }
        }
        catch (Exception ex)
        {
            PK_iNguoidungID = 0;
        }
        return PK_iNguoidungID;
    }

    /// <summary>
    /// Hiện tên khóa học theo ID KhoahocBaihoc
    /// </summary>
    /// <author>liemqv</author>
    /// <create>08/06/2012</create>
    ///
    public String sTenkhoahoc(Int64 PK_iKhoahoc_BaihocID)
    {
        Khoahoc_BaihocEntity oKhoahocBaihoc = Khoahoc_BaihocBRL.GetOne(PK_iKhoahoc_BaihocID);
        return BaihocBRL.GetOne(oKhoahocBaihoc.FK_iBaihocID).sTenbaihoc;
    }

    /// <summary>
    /// Lấy tên người dùng theo ID
    /// </summary>
    /// <author>liemqv</author>
    /// <create>07/06/2012</create>
    /// 
    public String GetTenNguoidungByID(Int64 PK_iNguoidungID)
    {
        return NguoidungBRL.GetOne(PK_iNguoidungID).sHoten;
    }

    /// <summary>
    /// Lấy ID của khóa học đang được chọn
    /// </summary>
    /// <author>liemqv</author>
    /// <create>07/06/2012</create>
    /// 
    public Int64 GetPK_iKhoahocID()
    {
        Int64 iRes = 0;
        if (Session["PK_iKhoahocID_Lich"] != null)
        {
            iRes = Int64.Parse(Session["PK_iKhoahocID_Lich"].ToString());
        }
        else
        {
            iRes = Int64.Parse(ddlKhoahocxem.SelectedValue);
            Session["PK_iKhoahocID_Lich"] = iRes;
        }
        return iRes;
    }
    /// <summary>
    /// Nạp gridview lịch học
    /// </summary>
    /// <author>liemqv</author>
    /// <create>07/06/2012</create>
    /// 
    public void napLichhoc()
    {
        Int64 PK_iKhoahocID = GetPK_iKhoahocID();
        List<LichhocEntity> lstLich = LichhocBRL.GetByFK_iKhoahocID(PK_iKhoahocID);
        grvLichhoc.DataSource = lstLich;
        grvLichhoc.DataKeyNames = new string[] { "PK_iLichhocID" };
        grvLichhoc.DataBind();
        lstLich = null;
    }

    /// <summary>
    /// Nạp JavaScript FullCalendar lịch học
    /// </summary>
    /// <author>liemqv</author>
    /// <create>08/06/2012</create>
    ///
    public String sCalendarEnvent()
    {
        List<LichhocEntity> lstLichhoc = LichhocBRL.GetByFK_iKhoahocID(GetPK_iKhoahocID());
        String sRes = "";
        String sTmp = "";
        Khoahoc_BaihocEntity oKhoahocBaihoc = null;
        BaihocEntity oBaihoc = null;
        String sTitle = "";
        String sGiohoc = "";
        short iYear = 0;
        short iMonth = 0;
        short iDay = 0;
        for(int i=0; i<lstLichhoc.Count-1; ++i)
        {
            oKhoahocBaihoc = Khoahoc_BaihocBRL.GetOne(lstLichhoc[i].FK_iKhoahoc_BaihocID);
            oBaihoc = BaihocBRL.GetOne(oKhoahocBaihoc.FK_iBaihocID);
            sTitle = oBaihoc.sTenbaihoc; // +" " + lstLichhoc[i].dGiobatdau.ToString("HH:mm") + "-" + lstLichhoc[i].dGioketthuc.ToString("HH:mm");
            iYear = short.Parse(lstLichhoc[i].dNgayhoc.ToString("yyyy"));
            iMonth = short.Parse(lstLichhoc[i].dNgayhoc.ToString("MM"));
            iMonth -= 1;
            sGiohoc = " (" + lstLichhoc[i].dGiobatdau.ToString("HH:mm") + " -> " + lstLichhoc[i].dGioketthuc.ToString("HH:mm") + ")";
            iDay = short.Parse(lstLichhoc[i].dNgayhoc.ToString("dd"));
            sTmp = String.Format(@"title: '{0}',start: new Date({1}, {2}, {3})", sTitle + sGiohoc, iYear, iMonth, iDay);
            sTmp = "{" + sTmp + "},";
            sRes += sTmp;
            sTmp = "";
        }
        oKhoahocBaihoc = Khoahoc_BaihocBRL.GetOne(lstLichhoc[lstLichhoc.Count-1].FK_iKhoahoc_BaihocID);
        oBaihoc = BaihocBRL.GetOne(oKhoahocBaihoc.FK_iBaihocID);
        sTitle = oBaihoc.sTenbaihoc; // +" " + lstLichhoc[lstLichhoc.Count - 1].dGiobatdau.ToString("HH:mm") + "-" + lstLichhoc[lstLichhoc.Count - 1].dGioketthuc.ToString("HH:mm");
        iYear = short.Parse(lstLichhoc[lstLichhoc.Count-1].dNgayhoc.ToString("yyyy"));
        iMonth = short.Parse(lstLichhoc[lstLichhoc.Count-1].dNgayhoc.ToString("MM"));
        iMonth -= 1;
        sGiohoc = " (" + lstLichhoc[lstLichhoc.Count - 1].dGiobatdau.ToString("HH:mm") + " -> " + lstLichhoc[lstLichhoc.Count - 1].dGioketthuc.ToString("HH:mm") + ")";
        iDay = short.Parse(lstLichhoc[lstLichhoc.Count-1].dNgayhoc.ToString("dd"));
        sTmp += String.Format(@"title: '{0}',start: new Date({1}, {2}, {3})", sTitle + sGiohoc, iYear, iMonth, iDay);
        sTmp = "{" + sTmp + "}";
        sRes += sTmp;
        return sRes;
    }

    /// <summary>
    /// Nạp lại from cập nhật lịch
    /// </summary>
    /// <author>liemqv</author>
    /// <create>08/06/2012</create>
    /// 
    public void resetForm()
    {
        txtGhichu.Text = "";
        txtNgayhoc.Text = DateTime.Now.ToString("dd/MM/yyyy");
        ddlBaihoc.SelectedItem.Selected = false;
        ddlBaihoc.Items[0].Selected = true;
    }

    /// <summary>
    /// Nạp lại from theo ID của lịch học
    /// </summary>
    /// <author>liemqv</author>
    /// <create>08/06/2012</create>
    /// 
    public void napForm(Int64 PK_iLichhocID)
    {
        LichhocEntity oLichhoc = LichhocBRL.GetOne(PK_iLichhocID);
        Khoahoc_BaihocEntity oKhoahocBaihoc = Khoahoc_BaihocBRL.GetOne(oLichhoc.FK_iKhoahoc_BaihocID);
        ddlBaihoc.SelectedItem.Selected = false;
        ddlBaihoc.Items.FindByValue(oKhoahocBaihoc.FK_iBaihocID.ToString()).Selected = true;
        txtNgayhoc.Text = oLichhoc.dNgayhoc.ToString("dd/MM/yyyy");
        txtGiobatdau.Text = oLichhoc.dGiobatdau.ToString("HH:mm");
        txtGioketthuc.Text = oLichhoc.dGioketthuc.ToString("HH:mm");
        ddlTrangthai.SelectedItem.Selected = false;
        ddlTrangthai.Items.FindByValue(oLichhoc.iTrangthai.ToString()).Selected = true;
        txtGhichu.Text = oLichhoc.sGhichu;
    }


    protected void lbtnAdd_Click(object sender, EventArgs e)
    {
        btnOK.CommandName = "Add";
        panEdit.Visible = true;
        resetForm();
    }
    protected void lbtnDelete_Click(object sender, EventArgs e)
    {

    }
    protected void ddlKhoahocxem_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["PK_iKhoahocID_Lich"] = ddlKhoahocxem.SelectedValue;
        ddlKhoahocxem.Items.FindByValue(ddlKhoahocxem.SelectedValue);
        napLichhoc();
    }
    protected void grvLichhoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvLichhoc.PageIndex = e.NewPageIndex;
        napLichhoc();
    }
    protected void grvLichhoc_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int iIndex = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = grvLichhoc.Rows[iIndex];
        Int64 PK_iLichhocID = Int64.Parse(grvLichhoc.DataKeys[iIndex]["PK_iLichhocID"].ToString());
        if (e.CommandName == "Sua")
        {
            btnOK.CommandName = "Edit";
            btnOK.Text = "<span>Sửa<span>";
            btnOK.CommandArgument = PK_iLichhocID.ToString();
            btnOK.CausesValidation = false;
            panEdit.Visible = true;
            napForm(PK_iLichhocID);
        }
        else
            if (e.CommandName == "Xoa")
            {
                if (LichhocBRL.Remove(PK_iLichhocID) == true)
                {
                    divThongbao.InnerHtml = "<p class='info' id='success'><span class='info_inner'>Xóa thành công!</span></p>";
                }
                else
                {
                    divThongbao.InnerHtml = "<p class='info' id='error'><span class='info_inner'>Xóa thất bại!</span></p>";
                }
            }
    }
    protected void grvLichhoc_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortDirection"] == null)
            ViewState["SortDirection"] = "ASC";
        else
        {
            ViewState["SortDirection"] = ViewState["SortDirection"].ToString() == "ASC" ? "DESC" : "ASC";
        }
        napLichhoc();
    }
    protected void grvLichhoc_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        LichhocEntity oLichhoc = new LichhocEntity();
        oLichhoc.FK_iNguoicapnhat = iGetUserLogin();
        oLichhoc.iTrangthai = short.Parse(ddlTrangthai.SelectedValue);
        oLichhoc.sGhichu = txtGhichu.Text;
        oLichhoc.dNgaytao = DateTime.Now;
        oLichhoc.FK_iNguoitao = iGetUserLogin();
        oLichhoc.dNgaycapnhat = DateTime.Now;
        oLichhoc.dNgayhoc = DateTime.Parse(txtNgayhoc.Text);
        oLichhoc.dGiobatdau = DateTime.Parse(txtGiobatdau.Text);
        oLichhoc.dGioketthuc = DateTime.Parse(txtGioketthuc.Text);

        Int64 PK_iBaihocID = Int64.Parse(ddlBaihoc.SelectedValue);
        Int64 PK_iKhoahocID = Int64.Parse(ddlKhoahocxem.SelectedValue);
        Khoahoc_BaihocEntity oKhoahocBaihoc = Khoahoc_BaihocBRL.GetByFK_iKhoahocIDAndFK_iBaihocID(PK_iKhoahocID, PK_iBaihocID)[0];
        oLichhoc.FK_iKhoahoc_BaihocID =  oKhoahocBaihoc.PK_iKhoahoc_BaihocID;
        oKhoahocBaihoc = null;

        if(btnOK.CommandName == "Add")
        {
            Int64 PK_iLichhocID = LichhocBRL.Add(oLichhoc);
            if(PK_iLichhocID > 0)
            {
                divThongbao.InnerHtml = "<p class='info' id='success'><span class='info_inner'>Thêm thành công!</span></p>";
            }
            else
            {
                divThongbao.InnerHtml = "<p class='info' id='error'><span class='info_inner'>Thêm thất bại!</span></p>";
            }
        }
        else
            if (btnOK.CommandName == "Edit")
            {
                Int64 PK_iLichhocID = Int64.Parse(btnOK.CommandArgument);
                LichhocEntity oLichhocOld = LichhocBRL.GetOne(PK_iLichhocID);
                oLichhoc.PK_iLichhocID = oLichhocOld.PK_iLichhocID;
                oLichhoc.FK_iNguoitao = oLichhocOld.FK_iNguoitao;
                oLichhoc.dNgaytao = oLichhocOld.dNgaytao;
                if (LichhocBRL.Edit(oLichhoc) == true)
                {
                    divThongbao.InnerHtml = "<p class='info' id='success'><span class='info_inner'>Sửa thành công!</span></p>";
                }
                else
                {
                    divThongbao.InnerHtml = "<p class='info' id='error'><span class='info_inner'>Sửa thất bại!</span></p>";
                }
            }
        panEdit.Visible = false;
        napLichhoc();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        panEdit.Visible = false;
    }
}